package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.ProdAttributeDto;
import com.yuke.cloud.service.pmc.dto.ProdCategoryAttributeDto;
import com.yuke.cloud.service.pmc.dto.ProdCategoryAttributeListDto;
import com.yuke.cloud.service.pmc.dto.ProdIdsDto;
import com.yuke.cloud.service.pmc.entity.ProdCategoryAttribute;
import com.yuke.cloud.service.pmc.service.ProdCategoryAttributeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
* Created by zys on 2018/11/16.
*/
@Slf4j
@Api(value = "WEB - ProdCategoryAttributeController", tags = "处理ProdCategoryAttribute相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/category_attribute")
public class ProdCategoryAttributeController extends BaseController {
    @Resource
    private ProdCategoryAttributeService prodCategoryAttributeService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条或多条记录")
    public Wrapper add(@ApiParam(name = "prodCategoryAttributeListDto", value = "该数据记录的POJO") @RequestBody ProdCategoryAttribute[] prodCategoryAttributes) {
        for (int i = 0 ; i<prodCategoryAttributes.length ; i++) {
            prodCategoryAttributeService.save(prodCategoryAttributes[i]);
        }
//        Arrays.asList(prodCategoryAttributes).stream().map(prodCategoryAttribute -> prodCategoryAttributeService.save(prodCategoryAttribute));
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        prodCategoryAttributeService.deleteById(id);
        return WrapMapper.ok();
    }

    @PostMapping("/ids")
    @ApiOperation(httpMethod = "POST", value = "根据id删除多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "attributeIds", value = "记录id") @RequestBody ProdIdsDto prodIdsDto) {
        prodCategoryAttributeService.deleteByIds(prodIdsDto.getIds());
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodCategoryAttribute", value = "需要修改的内容") @RequestBody ProdCategoryAttribute prodCategoryAttribute) {
        prodCategoryAttributeService.update(prodCategoryAttribute);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdCategoryAttribute> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdCategoryAttribute prodCategoryAttribute = prodCategoryAttributeService.findById(id);
        return WrapMapper.ok(prodCategoryAttribute);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdCategoryAttribute>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdCategoryAttribute> list = prodCategoryAttributeService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    //通过分类id获取所有已绑定的属性
    @GetMapping("/categoryId/bind/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "通过分类id获取所有已绑定的属性")
    public Wrapper<PageInfo<ProdCategoryAttributeDto>> getBindByCategoryId(@ApiParam(name = "categoryId", value = "记录id") @PathVariable Long categoryId,
                                                                       @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                       @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdCategoryAttributeDto> prodCategoryAttributeDtos = prodCategoryAttributeService.getBindByCategoryId(categoryId);
        PageInfo pageInfo = new PageInfo(prodCategoryAttributeDtos);
        return WrapMapper.ok(pageInfo);
    }

    //通过分类id获取所有未绑定的属性
    @GetMapping("/categoryId/unbound/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "通过分类id获取所有未绑定的属性")
    public Wrapper<PageInfo<ProdAttributeDto>> getUnboundByCategoryId(@ApiParam(name = "categoryId", value = "记录id") @PathVariable Long categoryId,
                                                                  @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                  @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdAttributeDto> prodAttributeDtos = prodCategoryAttributeService.getUnboundByCategoryId(categoryId);
        PageInfo pageInfo = new PageInfo(prodAttributeDtos);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/categoryId/unbound/condition/{categoryId}")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdAttributeDto>> getUnboundByCondition(@ApiParam(name = "categoryId", value = "记录id") @PathVariable Long categoryId,
                                                                   @ApiParam(name = "prodCategoryAttributeDto", value = "记录查询条件") @RequestBody ProdCategoryAttributeDto prodCategoryAttributeDto,
                                                                   @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                   @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdAttributeDto> list = prodCategoryAttributeService.getUnboundByCondition(categoryId, prodCategoryAttributeDto.getCategoryId(), prodCategoryAttributeDto.getAttributeName());
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }
}
